
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * ***********************************************************
 * Copyright  2019 八维通科技有限公司 Inc.All rights reserved.  *
 * ***********************************************************
 * <p>
 * description:
 *
 * @author zjf
 * @date 2020-10-22 22:16:24
 */
public class FileOpeartor {

    @Test
    public void mainTest() throws Exception {
        String fileDir = "/Users/zhongjiafeng/Desktop/OSChina/publish/boot-web-webwork/src/main/resources/privateKey";
//        List<String> contentList = readFile(fileDir);
        readBigFile(fileDir);
    }

    /**
     * @param fileDir 大文件对象
     * @return
     * @throws Exception
     */
    public Boolean readBigFile(String fileDir) throws Exception {
        File file = new File(fileDir);
        if (!file.exists()) {
            System.out.println("文件不存在");
            return false;
        }
        //假设只能存5行数据 超过五行的要等到下一次
        int initialCapacity = 5;
        List<String> contentList = new ArrayList<>(initialCapacity);

        // 读取的文件还是这个文件
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line;
            int count = 0;
            while ((line = br.readLine()) != null) {
                contentList.add(line.trim());
                // 当达到了5条之后 执行下打印
                if (contentList.size() == initialCapacity) {
                    count++;
                    System.out.println("当前第" + count + "次打印");
                    printContent(contentList);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (br != null) {
                br.close();
            }
        }

        return true;
    }

    private void printContent(List<String> contentList) {
        for (String content : contentList) {
            System.out.println(content);
        }
        // 清除掉之前的5条
        contentList.clear();
    }

    /**
     * 单一文件读取少量内容 可以这么操作 , 当文件过大的时候 为了减少内存消耗得拆
     *
     * @param fileDir
     * @return
     * @throws Exception
     */
    public List<String> readFile(String fileDir) throws Exception {
        File file = new File(fileDir);
        if (!file.exists()) {
            System.out.println("文件不存在");
            return new ArrayList<>();
        }
        List<String> contentList = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                contentList.add(line.trim());
            }
        } catch (FileNotFoundException e) {
            System.out.println("出现了异常...");
            e.printStackTrace();
        } finally {
            if (br != null) {
                br.close();
            }
        }
        // 返回文本里面的内容
        return contentList;
    }

    /**
     * 初级版本的写文件
     *
     * @throws Exception
     */
    @Test
    public void writeFile() throws Exception {
        String fileDir = "/Users/zhongjiafeng/Desktop/OSChina/publish/boot-web-webwork/src/main/resources/tempFile";
        File tempFile = new File(fileDir);
        if (tempFile.exists()) {
            tempFile.delete();
        }
        tempFile.createNewFile();

        FileWriter fw = null;
        try {
            fw = new FileWriter(tempFile);
            String content = "hello world";
            fw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                fw.close();
            }
        }
    }


}
